package com.minimalist.common.feign.fallback.lindi;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationtemplatecontract.LindiTradeApplicationTemplateContractQueryVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationtemplatecontract.LindiTradeApplicationTemplateContractVO;
import com.minimalist.common.feign.client.lindi.LindiTradeApplicationTemplateContractFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 林地交易申请合同模板 Feign 客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class LindiTradeApplicationTemplateContractFeignClientFallbackFactory extends BaseFallbackFactory<LindiTradeApplicationTemplateContractFeignClient>{
    private static final Logger log = LoggerFactory.getLogger(LindiTradeApplicationTemplateContractFeignClientFallbackFactory.class);


    @Override
    public LindiTradeApplicationTemplateContractFeignClient create(Throwable cause){
        return new LindiTradeApplicationTemplateContractFeignClient(){
            @Override
            public R<?> addTradeApplicationTemplateContract(LindiTradeApplicationTemplateContractVO tradeApplicationTemplateContractVO){
                logServiceUnavailable("添加交易申请合同模板", tradeApplicationTemplateContractVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "添加交易申请合同模板失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> deleteTradeApplicationTemplateContractByTradeApplicationTemplateContractId(Long tradeApplicationTemplateContractId){
                logServiceUnavailable("删除交易申请合同模板", tradeApplicationTemplateContractId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "删除交易申请合同模板失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> updateTradeApplicationTemplateContractByTradeApplicationTemplateContractId(LindiTradeApplicationTemplateContractVO tradeApplicationTemplateContractVO){
                logServiceUnavailable("修改交易申请合同模板", tradeApplicationTemplateContractVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "修改交易申请合同模板失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<PageResp<LindiTradeApplicationTemplateContractVO>> getPageTradeApplicationTemplateContractList(LindiTradeApplicationTemplateContractQueryVO queryVO){
                logServiceUnavailable("分页查询交易申请合同模板列表", queryVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "分页查询交易申请合同模板列表失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiTradeApplicationTemplateContractVO> getTradeApplicationTemplateContractByTradeApplicationTemplateContractId(Long tradeApplicationTemplateContractId){
                logServiceUnavailable("获取交易申请合同模板详情", tradeApplicationTemplateContractId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取交易申请合同模板详情失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> deleteTradeApplicationTemplateContractFileByFileId(Long fileId){
                logServiceUnavailable("删除交易申请合同模板文件", fileId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "删除交易申请合同模板文件失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<LindiContractTemplateVO>> getContractTemplateList(){
                logServiceUnavailable("获取合同模板列表", cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取合同模板列表失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> generateTemplateContract(Long tradeApplicationId, Long contractTemplateId, String contractType, Long createId, Long tenantId){
                logServiceUnavailable("生成合同", "tradeApplicationId: "
                        + tradeApplicationId + ", contractTemplateId: "
                        + contractTemplateId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "生成合同失败，服务不可用: " + cause.getMessage());
            }
        };
    }
}
